package com.yuexiao.config;

import com.alibaba.dubbo.rpc.RpcContext;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName:MyMetaObjecthandler
 * @Auther: YUEXIAO
 * @Description: 自定义元数据对象处理器
 * @Date: 2022/8/11 16:20
 * @Version: v1.0
 */
@Component
@Slf4j
public class MyMetaObjecthandler implements MetaObjectHandler {

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 插入操作自动填充
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段自动填充[insert]");
        log.info(metaObject.toString());

        Object loginId = redisUtil.get("loginId");
        log.info("填充id:{}",loginId);

        //填充时间
        metaObject.setValue("createTime", new Date());
        metaObject.setValue("updateTime", new Date());
        //填充id
        metaObject.setValue("createUser", Long.valueOf(String.valueOf(loginId)));
        metaObject.setValue("updateUser", Long.valueOf(String.valueOf(loginId)));
    }

    /**
     * 更新操作，自动填充
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充[update]");
        log.info(metaObject.toString());

        Object loginId = redisUtil.get("loginId");

        //填充数据
        metaObject.setValue("updateTime", new Date());
        metaObject.setValue("updateUser", Long.valueOf(String.valueOf(loginId)));
    }
}
